package com.compass.doctor.dialog

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.activity.viewModels
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.GridLayoutManager
import com.compass.doctor.R
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.callback.OnDialogBottomClick
import com.compass.doctor.databinding.DialogInquiryOrderBinding
import com.compass.doctor.preview.PreviewPicActivity
import com.compass.doctor.ui.home.DoctorListActivity
import com.compass.doctor.ui.home.InquiryOrderActivity
import com.compass.doctor.ui.home.adapter.PatientPictureAdapter
import com.compass.doctor.ui.home.viewmodel.InquiryOrderViewModel
import com.compass.doctor.ui.mine.CaSignatureActivity
import com.compass.doctor.ui.patient.PatientChatActivity
import com.compass.framework.base.BaseDialog
import com.compass.framework.base.BaseDialogFragment
import com.compass.framework.constant.CLINICAL_RECEPTION
import com.compass.framework.constant.SET_INDEX
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.glide.setUrlCircle
import com.compass.framework.manager.AppManager
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.model.InquiryMessage
import com.compass.framework.utils.DateUtils
import com.compass.framework.utils.LiveDataBus
import com.compass.framework.utils.dpToPx
import com.compass.framework.utils.getDrawableResource
import com.luck.picture.lib.entity.LocalMedia
import com.netease.yunxin.kit.corekit.im.model.UserInfo

/**
 * Description： 问诊单弹窗
 * Date:2023/12/11 10:53
 */
class InquiryOrderDialog {
    class Builder(
        activity: FragmentActivity,
        id: String,
        onCustomButtonClick: OnDialogBottomClick
    ) :
        BaseDialogFragment.Builder<Builder>(activity) {

        private var onCustomButtonClick: OnDialogBottomClick? = null
        private var orderId: String
        private val inquiryOrderViewModel: InquiryOrderViewModel by activity.viewModels()
        private var inquiryMessage: InquiryMessage? = null
        private var pics: ArrayList<String> = ArrayList()
        private lateinit var patientPictureAdapter: PatientPictureAdapter
        private val mBinding: DialogInquiryOrderBinding = DialogInquiryOrderBinding.inflate(
            LayoutInflater.from(activity)
        )

        init {
            this.orderId = id
            this.onCustomButtonClick = onCustomButtonClick
            initView()
            initData()
        }

        private fun initView() {
            setContentView(mBinding.root)
            setWidth(ViewGroup.LayoutParams.MATCH_PARENT)
            setHeight(ViewGroup.LayoutParams.WRAP_CONTENT)
            //设置最大高度
            setMaxHeight((AppManager.getScreenHeightPx()*0.7).toInt())

            setAnimStyle(BaseDialog.AnimStyle.BOTTOM)
            gravity = Gravity.BOTTOM

            mBinding.imgClose.onClick {
                dismiss()
            }
            mBinding.tvClinicalReception.onClick {
                 //接诊
                inquiryMessage?.let {
                    onCustomButtonClick?.onDialogButtonCallBack(it,"Reception")
                }
                dismiss()
            }
            mBinding.tvTransferTreatment.onClick {
                //转诊
                inquiryMessage?.let {
                    onCustomButtonClick?.onDialogButtonCallBack(it,"Treatment")
                }
                dismiss()

            }
            mBinding.tvRefusal.onClick {
                //拒诊
                inquiryMessage?.let {
                    onCustomButtonClick?.onDialogButtonCallBack(it,"Refusal")
                }
                dismiss()

            }
            if (UserInfoManager.getUserInfo()?.appHospitalConfigRespVO?.isAllowReferral == "1") {
                mBinding.tvTransferTreatment.visible()
            } else {
                mBinding.tvTransferTreatment.gone()
            }

            initRcyPicture()
        }

        /**
         * 初始化数据
         */
        private fun initData() {
            inquiryOrderViewModel.inquiryMessageLiveData.observe(activity) {
                it?.let {
                    setData(it)
                }
            }
            inquiryOrderViewModel.getUrgencyDetails(orderId)
        }



        @SuppressLint("SetTextI18n")
        private fun setData(message: InquiryMessage) {
            inquiryMessage = message
            mBinding.ivHead.setUrlCircle(message.patientAvatar)
            mBinding.tvName.text = message.patientName
            mBinding.tvAge.text = message.patientAge
            mBinding.ivSex.background =
                if (message.patientSex == 1) getDrawableResource(R.mipmap.icon_male) else getDrawableResource(
                    R.mipmap.icon_female
                )
            mBinding.tvCreationTime.text =
                DateUtils.conversionTime(message.createTime, DateUtils.FORMAT_TWO)
            mBinding.descriptionContent.text = message.patientSymptom
            pics = message.infectedParts as ArrayList<String>
            patientPictureAdapter.setData(pics)
        }


        private fun initRcyPicture() {
            patientPictureAdapter = PatientPictureAdapter(1)
            mBinding.rcyPicture.apply {
                layoutManager = GridLayoutManager(activity, 4)
                adapter = patientPictureAdapter
                addItemDecoration(NormalItemDecoration().apply {
                    setBounds(top = dpToPx(10), bottom = dpToPx(10))
                })
            }
            patientPictureAdapter.onItemClickListener = { view: View, position: Int ->
                var loacls = ArrayList<LocalMedia>()
                var localMedia: LocalMedia
                for (pic in pics) {
                    localMedia = LocalMedia()
                    localMedia.path = pic
                    loacls.add(localMedia)
                }
                if (loacls.isNotEmpty()) {
                    PreviewPicActivity.start(activity, position, loacls)
                }
            }

        }

    }
}